package com.qyzmt.service.impl;

import com.qyzmt.mapper.LoginMapper;
import com.qyzmt.pojo.LoginToken;
import com.qyzmt.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Random;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private LoginMapper loginMapper;
    @Override
    public LoginToken login(LoginToken loginToken) {
        String username = loginToken.getUsername();
        String password = loginToken.getPassword();
        int authid = 0;
        int authtoken = 0;
        // 接收Mapper层的查询结果。查询到则返回1.
        int bl = loginMapper.login(username,password);
        if(bl > 0){
            // 登陆成功，分配id与token.
            Random random = new Random();
            authid = random.nextInt(4241 - 1241 + 1) + 1241;  // 1~6随机骰子
            authtoken = (authid - 7) * 2;
            return new LoginToken(username,password,Integer.toString(authid),Integer.toString(authtoken));
        }
        else {
            // 登录失败
            authid = -1;
            authtoken = -1;
            return new LoginToken(username,password,Integer.toString(authid),Integer.toString(authtoken));
        }


    }
}
